package model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import view.AddWindow;

/**
 * Create a new text file and add the new contact profile created by the user in the app with the other contacts
 * @author Marie Morin
 *
 */

public class SaveModel {
	
	AddWindow w;
	ArrayList<Contact> contactsList = new ArrayList<Contact>();
	ArrayList<Contact> newContactsList = new ArrayList<Contact>();
	String date;
	
	public SaveModel(AddWindow w) throws FileNotFoundException, UnsupportedEncodingException{
		
		this.w = w;
		ContactListModel c = new ContactListModel();
		this.contactsList = c.getContactsList();
		
		String aname=" ";
		String name= " ";
		String fname= " ";
		String phone= " ";
		String professionalPhone=" ";
		String homeAddress=" ";
		String professionalAddress=" ";
		String email="-";
		String group=" ";
		String website="-";
		String date = new SimpleDateFormat("dd-MM-yyyy, hh:mm:ss").format(new Date());
		
		if (!w.getJtf_name().getText().equals(""))
			name = w.getJtf_name().getText();
		
		if (!w.getJtf_fname().getText().equals(""))
			fname=w.getJtf_fname().getText();
		
			
		if (!w.getJtf_phone().getText().equals(""))
			phone =w.getJtf_phone().getText();
		
		if (!w.getJtf_professionalPhone().getText().equals(""))
			professionalPhone = w.getJtf_professionalPhone().getText();
		
		if (!w.getJtf_homeAddress().getText().equals(""))
			homeAddress = w.getJtf_homeAddress().getText();
		
		if (!w.getJtf_professionalAddress().getText().equals(""))
			professionalAddress = w.getJtf_professionalAddress().getText();
		
		if (!w.getJtf_email().getText().equals(""))
			email = w.getJtf_email().getText();
			
		if (!w.getJtf_group().getText().equals(""))
			group = w.getJtf_group().getText();
		
		if (!w.getJtf_website().getText().equals(""))
			website = w.getJtf_website().getText();
		
		
		aname = name + " " + fname;
		Contact contact = new Contact(name, fname, aname, phone, professionalPhone, homeAddress, professionalAddress, email, group, website, date);
		
		this.contactsList.add(contact);
		
		
		PrintWriter writer = new PrintWriter("test.txt", "UTF-8");
		
		if (contactsList.size()>0){
			

			for(int i=0; i<contactsList.size()-1; i++){
			
				writer.println(contactsList.get(i).getName());
				writer.println(contactsList.get(i).getFname());
				writer.println(contactsList.get(i).getPhone());
				writer.println(contactsList.get(i).getEmail());
				writer.println(contactsList.get(i).getWebsite());
				writer.println(contactsList.get(i).getHomeAddress());
				writer.println(contactsList.get(i).getProfessionalPhone());
				writer.println(contactsList.get(i).getProfessionalAddress());
				writer.println(contactsList.get(i).getGroup());
				writer.println(contactsList.get(i).getDate());
				
			}
			
			writer.println(contactsList.get(contactsList.size()-1).getName());
			writer.println(contactsList.get(contactsList.size()-1).getFname());
			writer.println(contactsList.get(contactsList.size()-1).getPhone());
			writer.println(contactsList.get(contactsList.size()-1).getEmail());
			writer.println(contactsList.get(contactsList.size()-1).getWebsite());
			writer.println(contactsList.get(contactsList.size()-1).getHomeAddress());
			writer.println(contactsList.get(contactsList.size()-1).getProfessionalPhone());
			writer.println(contactsList.get(contactsList.size()-1).getProfessionalAddress());
			writer.println(contactsList.get(contactsList.size()-1).getGroup());
			writer.print(contactsList.get(contactsList.size()-1).getDate());

			writer.close();	
			
		}
			
	}

}
